var selectedRow = '';
var isInit = true;

initLaydate();

function initLaydate() {
    laydate.render({
        elem: '#timeInterval'
        ,type: 'datetime'
        ,range: true //或 range: '~' 来自定义分割字符
        // ,value: dateFtt("yyyy-MM-dd hh:mm:ss", preDate) + ' - ' + dateFtt("yyyy-MM-dd hh:mm:ss", curDate)
        //js赋值重置失效，改为就是怕、页面赋值
        ,isInitValue: true //是否允许填充初始值，默认为 true
    });
}

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoPage();
        },
        /*'click .isvInfoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openCommonIsvPage(row.isvPid, 'queryDoc');
        },*/
    }
    initTextSearch("#orgName",basePath + 'checkOrgTeam/getOrgList?t='+ (new Date()).getTime()+'&name=',['orgCode','orgName'],{'orgCode':'机构编码','orgName':'机构名称'},'orgCode','orgName',true,selectOrgValue,orgChange);
});

function doRefresh() {
    selectedRow = '';
    var docName = $('#docName').val();
    var checkType = $('#checkType').val();
    var teamName = $('#teamName').val();
    var orgCode = $('#orgCode').val();
    var result = $('#result').val();
    var gmtStart = '';
    var gmtEnd = '';
    var timeInterval = $("#timeInterval").val();
    if(timeInterval){
        var dates = timeInterval.split(' - ');
        if(dates && dates.length >1){
            gmtStart = dates[0];
            gmtEnd = dates[1];
        }
    }
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'checkOrgTeam/pageFindCheckedOrgTeamData?t=' + new Date().getTime(),
        query: {
            docName : docName,
            teamName : teamName,
            orgCode : orgCode,
            result : result,
            gmtStart : gmtStart,
            gmtEnd : gmtEnd,
            checkType : checkType
        },
    });
}

function getParams(params) {
    var docName = $('#docName').val();
    var checkType = $('#checkType').val();
    var teamName = $('#teamName').val();
    var orgCode = $('#orgCode').val();
    var result = $('#result').val();
    var gmtStart = '';
    var gmtEnd = '';
    var timeInterval = $("#timeInterval").val();
    if(timeInterval){
        var dates = timeInterval.split(' - ');
        if(dates && dates.length >1){
            gmtStart = dates[0];
            gmtEnd = dates[1];
        }
    }
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        docName : docName,
        checkType : checkType,
        teamName : teamName,
        orgCode : orgCode,
        result : result,
        gmtStart : gmtStart,
        gmtEnd : gmtEnd,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function openCheckInfoPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("查看"));
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'checkOrgTeam/openCheckOrgTeamPage?&id=' + selectedRow.id + '&isCheckExist=' + searchInfo.isCheckExist +
        '&type=info&page=2' + '&t=' + new Date().getTime() + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function openInfoPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'checkOrgTeam/openCheckOrgTeamPage?&id=' + selectedRow.id + '&isCheckExist=' + searchInfo.isCheckExist +
        '&type=info&page=2' + '&t=' + new Date().getTime() + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function formatCheckType(value, row, index) {
    return convertMapCodeText(checkTypeMap,value);
}

function formatCheckResultType(value, row, index) {
    return convertMapCodeText(checkResultTypeMap,value);
}

function formatDeptName(value, row, index) {
    return row.cDeptName ? row.cDeptName : row.pDeptName;
}

function formatDocMediLevel(value, row, index) {
    return convertListCodeText(mediLevelList, value, "dictCode", "dictName");
}

$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});

function orgChange() {
    //清空值
    $('#orgCode').val("");
}

function selectOrgValue(data) {
    //联动赋值
    if (data && data.orgCode && data.orgCode != '') {
        $('#orgCode').val(data.orgCode);
    }
}

function isvChange() {
    //清空值
    $('#isvPid').val("");
}

function selectIsvValue(data) {
    //联动赋值
    if (data && data.isvPid && data.isvPid != '') {
        $('#isvPid').val(data.isvPid);
    }
}